// SPDX-FileCopyrightText: 2023-2025 Sangfor Technologies Inc.
// SPDX-License-Identifier: Mulan PSL v2
package com.geniusai.aip.auth.controller;

import com.geniusai.aip.auth.domain.LoginEntity;
import com.geniusai.aip.auth.domain.ResponseTokenDTO;
import com.geniusai.aip.auth.service.LoginService;
import com.geniusai.aip.feature.common.web.Response;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author hml14563
 * @date 2021/3/20
 */
@RestController
@Slf4j
@RequestMapping("/v1")
public class AuthController {
    @Autowired
    private LoginService loginService;

    @ApiOperation(value = "认证登陆", httpMethod = "POST", notes = "认证登陆", produces = "application/json")
    @PostMapping("/login")
    public Response<ResponseTokenDTO> login(
            HttpServletRequest request, @RequestBody LoginEntity loginEntity, HttpServletResponse response
    ) {
        return Response.forSuccessWithData(loginService.login(request, loginEntity, response));
    }

    @ApiOperation(value = "认证退出", httpMethod = "POST", notes = "认证退出", produces = "application/json")
    @PostMapping("/logout")
    public Response releaseResource(
            HttpServletRequest request, HttpSession session
    ) {
        loginService.logout(request, session);
        return Response.forSuccess("用户成功退出");
    }

    @ApiOperation(value = "获取token", httpMethod = "GET", notes = "获取token", produces = "application/json")
    @GetMapping("/token")
    public Response<String> getToken(
            HttpServletRequest request, @RequestParam String username
    ) {
        return Response.forSuccessWithData(loginService.getToken(request, username));
    }

    @ApiOperation(value = "获取验证资格", httpMethod = "GET", notes = "判断验证资格", produces = "application/json")
    @GetMapping("/permission")
    public void getPermission(HttpServletRequest request, HttpServletResponse response) {
        loginService.getPermission(request, response);
    }
}
